package com.bcpapp.project.utils;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ssh.Sftp;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
public class FileUtils {

    private static void createFileOrDir() {

        // 创建文件 如果存在则跳过

        // FileUtil.touch(new File("D:\\java\\1.bcp"));

        // 创建目录 如果存在跳过

        FileUtil.mkdir("D:\\java");

    }
/*
* 时间格式化
*
* */
    public static String time(Date date){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(date);
        return format;
    }

    /*
    * 每10M分割数据
    *
    * */
    public static List<List> dataSize(List list) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(list);
        os.close();
        System.out.println("字节数：" + baos.size());
        NumberUtil.round(baos.size(), 4);
        int a  = baos.size() / 1048576;
        System.out.println("MB：" + baos.size()/ 1048576);
        Integer res =1;

        if (a >= 10){
           res = sizeCeil(a, 10)+1;
        }

        Integer res2 =list.size()/res;

        List<List> listList = ListUtil.partition(list, res2);


        return listList;
    }


    /*
    *
    * 数据向上求整
    * */
    public static Integer sizeCeil(Integer a,Integer b){


        Integer res =   a / b + (a % b != 0 ? 1 : 0);
        return    res;
    }
    public static List<String> getFiles(String path) {

        List<String> fileList =new ArrayList<>();
        File file = new File(path);
        // 如果这个路径是文件夹
        if (file.isDirectory()) {
            // 获取路径下的所有文件
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                // 如果还是文件夹 递归获取里面的文件 文件夹
                if (files[i].isDirectory()) {
                    System.out.println("目录：" + files[i].getPath());
                    getFiles(files[i].getPath());

                } else {
                    fileList.add(files[i].getPath());
                    System.out.println("文件：" + files[i].getName()); // files[i].getPath());
                }
            }

        } else {

            System.out.println("文件：" + file.getPath());

        }
        return  fileList;
    }

    public static void updateFile(String host,Integer port,String username,String password,List<String> fileNames,String onlinePath){
          Sftp sftp = null;
        try {
        // SFTP 为 SSH的一部份、所以端口用的是22
            //sftp = new Sftp("123.249.6.47",22,"sj_zy","ZYsj@2023!");
            sftp = new Sftp(host,port,username,password);
        // 第一个参数：上传的路径
        // 第二个参数：上传的文件、文件名是什么上传的就是什么名字
            for (String filename:fileNames
                 ) {
                boolean upload = sftp.upload(onlinePath, new File(filename));
                log.info(filename+"is already uploaded");
                FileUtil.del(new File(filename));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sftp != null) {
                sftp.close();
            }
        }
    }

    public static String readLastUpdateTime( String path){
        File file = null;
        try {
            file = ResourceUtils.getFile(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String content = FileReader.create(file).readString();;
        System.out.println(content);

        return content;

    }

    public static void  writeLastTime(String path,String liastTime){
        File file = null;
        try {
            file = ResourceUtils.getFile(path);
            FileUtil.writeString(liastTime,file, CharsetUtil.UTF_8);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }



    }


    //获取文档中数据分类文件
    public static Map<String,String> readFileData(String path){
        File file = null;
        try {
            file = ResourceUtils.getFile(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String content = FileReader.create(file).readString();;
        System.out.println(content);
        if (StrUtil.isNotBlank(content)){
            JSONObject resultJston = new JSONObject(content);
            Map<String,String> ageMap =   (Map) JSONUtil.parse(resultJston);
            System.out.println(ageMap);
            return ageMap;
        }

        return  null;
    }



}
